package org.chameleon.threadpool.pool;

import org.chameleon.lang.cache.Refreshable;
import org.chameleon.threadpool.entity.ThreadPoolConfig;

import java.util.concurrent.*;

public  class DynamicThreadPool extends ThreadPoolExecutor implements Refreshable<ThreadPoolConfig> {


    public DynamicThreadPool(ThreadPoolConfig threadPoolConfig){
        super(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaximumPoolSize(),
                threadPoolConfig.getKeepAliveTime(), threadPoolConfig.getUnit(), new LinkedBlockingQueue<>(threadPoolConfig.getWorkQueueSize()));
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
    }

    @Override
    protected void terminated() {
        super.terminated();
    }

    @Override
    public void refresh(ThreadPoolConfig threadPoolConfig) {
        this.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        this.setMaximumPoolSize(threadPoolConfig.getMaximumPoolSize());
        this.setKeepAliveTime(threadPoolConfig.getKeepAliveTime(),threadPoolConfig.getUnit());
    }
}
